﻿using System.Collections.Specialized;
using Castle.Components.DictionaryAdapter;
using Demo.ProductManager.Products;
using Demo.ProductManager.Products.Dto;
using Demo.Store.Application.Contracts.Store;
using Demo.Store.Application.Contracts.Store.Dto;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;

namespace Demo.Store.Application;

/// <summary>
/// 商城商品管理应用服务
/// </summary>
public class StoreProductAppService:ApplicationService,IStoreProductAppService
{
    private IProductAppService _productAppService;
    
    public StoreProductAppService(IProductAppService productAppService)
    {
        _productAppService = productAppService; 
    }
    /// <summary>
    /// 创建商品
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    public async Task<StoreProductDto> CreateAsync(StoreProductCreateDto dto)
    {
        var product = ObjectMapper.Map<StoreProductCreateDto, ProductDto>(dto);
        var ret= await _productAppService.CreateAsync(product);
        return ObjectMapper.Map<ProductDto, StoreProductDto>(ret);
    }
    
    /// <summary>
    /// 删除商品
    /// </summary>
    /// <param name="id">商品编号</param>
    /// <returns></returns>
    public async Task<bool> DeleteAsync(Guid id)
    {
        await _productAppService.DeleteAsync(id);
        return true;
    }

    /// <summary>
    /// 分页获取商品列表
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public async Task<PagedResultDto<StoreProductDto>> GetListAsync(PagedAndSortedResultRequestDto request)
    {
        var ret= await _productAppService.GetListAsync(request);
        return new PagedResultDto<StoreProductDto>()
        {
            TotalCount = ret.TotalCount,
            Items = ObjectMapper.Map<IReadOnlyList<ProductDto>, List<StoreProductDto>>(ret.Items)
        };
    }
}